package java9_23zy;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Test_2 {
    public static void main(String[] args) {
        Scanner in=new Scanner(System.in);
        SAXReader s=new SAXReader();
        try {
            Document r = s.read("D:\\aaa\\src\\java9_23zy\\products.xml");
            Element r1 = r.getRootElement();//获取更目录
            ArrayList<Shop> products = new ArrayList<>();
            List<Element> l = r1.selectNodes("product");
            for (Element e : l) {
                String itemno= e.selectSingleNode("itemno").getText();
                String name= e.selectSingleNode("name").getText();
                String price= e.selectSingleNode("price").getText();
                String stockcount= e.selectSingleNode("stockcount").getText();
                String unit= e.selectSingleNode("unit").getText();
                Shop s1=new Shop();
                s1.setItemno(itemno);
                s1.setName(name);
                s1.setPrice(price);
                s1.setStockcount(stockcount);
                s1.setUnit(unit);
                products.add(s1);
            }
            System.out.println("请输入商品编号");
            String s1 = in.next();
            for (Shop product : products) {
                    if (product.getItemno().equals(s1)){
                        System.out.println(product);
                        continue;
                    }
                }

        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }
}
